package com.axios.dao.impl;

import com.axios.dao.interfaces.JsonServerDAO;
import com.axios.models.pojo.JsonServer;
import com.axios.utils.DBHelper;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class JsonServerDAOImpl implements JsonServerDAO {

    // 返回所有的数据
    @Override
    public ArrayList<JsonServer> link() {
        ArrayList<JsonServer> jsonServers = new ArrayList<JsonServer>();
        // 开启连接
        Connection conn = DBHelper.getConn();

        try{
            String sql = "select * from jsonserver";
            ResultSet rs = DBHelper.executeQuery(sql,null);
            while(rs.next()){
                jsonServers.add(new JsonServer(
                        rs.getInt("id"),
                        rs.getString("name"),
                        rs.getString("sex"),
                        rs.getInt("age")
                ));
            }
        }catch (SQLException e) {
            throw new RuntimeException(e);
        }

        // 关闭连接
        DBHelper.closeConn();
        return jsonServers;
    }

    // 添加数据
    @Override
    public Integer add(JsonServer jsonServer) {
        Integer count = -1;
        // 开启连接
        Connection connection = DBHelper.getConn();

        // 判断该学生是否存在
        if(isExist(connection,jsonServer) == 0){
            // 编写添加SQL语句
            String sql = "insert into jsonserver(name,sex,age) values(?,?,?)";

            // 执行添加操作
            count = DBHelper.executeUpdate(sql,new Object[]{
                    jsonServer.getName(),
                    jsonServer.getSex(),
                    jsonServer.getAge()
            });
        }else{
            return 0;
        }

        // 关闭连接
        DBHelper.closeConn();
        return count;
    }

    // 删除数据
    @Override
    public Integer del(JsonServer jsonServer) {
        Integer count = -1;
        // 开启连接
        Connection connection = DBHelper.getConn();

        // 判断学生是否存在
        if(isExist(connection,jsonServer) == 0){
            return -1;
        }else{
            // 编写删除SQL语句
            String sql = "delete from jsonserver where id = ?";

            // 执行删除操作
            count = DBHelper.executeUpdate(sql,new Object[]{jsonServer.getId()});
        }

        // 关闭连接
        DBHelper.closeConn();
        return count;
    }

    // 修改数据
    @Override
    public Integer update(int id,JsonServer jsonServer) {
        Integer count = -1;
        // 开启连接
        Connection connection = DBHelper.getConn();

        // 判断学生是否存在
        if(isExist(connection,jsonServer) == 0){
            return 0;
        }else{
            // 编写修改的SQL语句
            String sql = "update jsonserver set name = ?,sex = ?,age = ? where id = ?";

            // 执行操作
            count = DBHelper.executeUpdate(sql,new Object[]{
                jsonServer.getName(),
                jsonServer.getSex(),
                jsonServer.getAge(),
                id
            });
        }
        // 关闭连接
        DBHelper.closeConn();
        return count;
    }

    // 判断是否存在
    private Integer isExist(Connection conn,JsonServer jsonServer){
        Integer count = 0;
        // 编写 SQL 语句
        String sql = "select count(id) id from jsonserver where id = ?";

        try{
            // 返回结果集
            ResultSet rs = DBHelper.executeQuery(sql,new Object[]{jsonServer.getId()});
            while(rs.next()){
                count = rs.getInt("id");
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return count;
    }

    public static void main(String[] args) {
        JsonServerDAO jsonServerDAO = new JsonServerDAOImpl();
        JsonServer jsonServer = new JsonServer(1,"胡国志10","女",20);
        Integer count = jsonServerDAO.update(1,jsonServer);
        System.out.println(count);
    }
}
